#include "MiniCrt.h"

int main(int argc,char* argv[])
{
	int i = 0;
	FILE* fp = NULL;
	char** v = (char**)mmalloc((argc * sizeof(char*)));
	for(i = 0;i < argc;++i)
	{
		v[i] = mmalloc(strlen(argv[i]) + 1);
		strcpy(v[i],argv[i]);
	}

	fp = fopen("test.txt","w");
	if(!fp)
	{
		return 0;
	}

	for(i = 0;i < argc;++i)
	{
		int len = strlen(v[i]);
		fwrite(&len,1,sizeof(int),fp);
		fwrite(v[i],1,len,fp);
	}
	fclose(fp);

	fp = fopen("text.txt","r");
	if(!fp)
	{
		return 0;
	}

	for(i = 0;i < argc;++i)
	{
		int len;
		char* buf;
		fread(&len,1,sizeof(int),fp);
		buf = mmalloc(len + 1);
		fread(buf,1,len,fp);
		buf[len] = '\0';
		printf("%d %s\n",len,buf);
		mfree(buf);
		mfree(v[i]);
	}

	fclose(fp);
}